﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using System.Web;

namespace MvcApplication.Models.Common
{
    /// <summary>
    /// 基础支持工具类
    /// </summary>
    public class BaseHelp
    {

        ///   <summary>
        ///   给一个字符串进行MD5加密
        ///   </summary>
        ///   <param   name="strText">待加密字符串</param>
        ///   <returns>加密后的字符串</returns>
        public static string MD5Encrypt(string strText, string charset)
        {
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] result = md5.ComputeHash(Encoding.GetEncoding(charset).GetBytes(strText));
            return System.BitConverter.ToString(result).Replace("-", "");
        }

        /// <summary>
        /// POST请求
        /// </summary>
        /// <param name="requestUrl">地址</param>
        /// <param name="json">传递的值</param>
        /// <returns></returns>
        public static string DoPost(string requestUrl, string json)
        {
            string responseJson = "";
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUrl);
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            byte[] postdatabyte = System.Text.Encoding.UTF8.GetBytes(json);
            request.ContentLength = postdatabyte.Length;

            Stream stream;
            stream = request.GetRequestStream();
            stream.Write(postdatabyte, 0, postdatabyte.Length); //设置请求主体的内容 
            stream.Close();

            // WebResponse response = request.GetResponse();
            HttpWebResponse res;
            try
            {
                res = (HttpWebResponse)request.GetResponse();
            }
            catch (WebException ex)
            {
                res = (HttpWebResponse)ex.Response;
            }
            using (StreamReader reader = new StreamReader(res.GetResponseStream(), System.Text.Encoding.UTF8))
            {
                responseJson = reader.ReadToEnd();
            }
            return responseJson;
        }
        /// <summary>
        /// 需要WebService支持Get调用
        /// </summary>
        public static string DoGet(String requestUrl)
        {
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(requestUrl);
            request.Method = "GET";
            request.Accept = "text/html, application/xhtml+xml, application/json, text/javascript, */*; q=0.01";
            request.Headers.Add("Accept-Language", "zh-cn,en-US,en;q=0.5");
            request.Headers.Add("Cache-Control", "no-cache");
            request.UserAgent = "DefaultUserAgent";
            request.ContentType = "application/x-www-form-urlencoded";
            SetWebRequest(request);
            // WebResponse response = request.GetResponse();
            WebResponse res;
            try
            {
                res = request.GetResponse();
            }
            catch (WebException ex)
            {
                res = (WebResponse)ex.Response;
            }
            StreamReader sr = new StreamReader(res.GetResponseStream(), Encoding.UTF8);
            String retXml = sr.ReadToEnd();
            sr.Close();
            return retXml;
        }
        /// <summary>
        /// 设置凭证与超时时间
        /// </summary>
        /// <param name="request"></param>
        private static void SetWebRequest(HttpWebRequest request)
        {
            request.Credentials = CredentialCache.DefaultCredentials;
            request.Timeout = 10000;
        }
    }
}